package utils;

import java.sql.*;
import java.util.ResourceBundle;

public class JdbcUtil {

    private static ResourceBundle resourceBundle;
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    static {
        resourceBundle = ResourceBundle.getBundle("db");
        driver = resourceBundle.getString("driver");
        url = resourceBundle.getString("url");
        username = resourceBundle.getString("username");
        password = resourceBundle.getString("password");
    }

    public static Connection getConnectionDB() {
        try {
            //注册驱动
            Class.forName(driver);
            //2.获取连接
            Connection connection = DriverManager.getConnection(url, username, password);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void releaseRes(Connection con, PreparedStatement pm, ResultSet rs) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (pm != null) {
            try {
                pm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
